package com.dunshanredis.controller;

import com.dunshanredis.pojo.Student;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author liwen406
 * @program: dunshan-pef
 * @description: indexpage
 * @date 2021-08-08 16:04:38
 */
@Log4j2
@Controller
@RequestMapping("/stu")
public class IndexController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("/test")
//    @RedisSelect()
    @ResponseBody
    public Object set(Student student) {
        System.out.println("参数" + student);
        redisTemplate.opsForValue().set("student", student.toString());
        HashMap<String, Object> map = new HashMap<>();
        map.put("ok", "调试");
        map.put("data", redisTemplate.opsForValue().get("student"));
        return map;
    }

    @PostMapping("/test/insert")
//    @RedisSelect()
    @ResponseBody
    public Object setInsert(@RequestBody Student student) {
        System.out.println("参数" + student);
        redisTemplate.opsForValue().set("student", student.toString());
        HashMap<String, Object> map = new HashMap<>();
        map.put("ok", "调试");
//        map.put("data", (Student) redisTemplate.opsForValue().get("student"));

        return map;
    }


    @GetMapping("/{key}")
//    @RedisSelect()
    @ResponseBody
    public Object get(@PathVariable String key) {
        Object o = redisTemplate.opsForValue().get(key);
        System.out.println("--" + o);
        return o;
    }

    @DeleteMapping("/{key}")
//    @RedisSelect()
    public boolean delete(@PathVariable String key) {
        Boolean res = redisTemplate.delete(key);
        return !redisTemplate.hasKey(key);
    }


    @GetMapping("/redis/{key}")
    public void setRedisDemo(HttpServletRequest request,@PathVariable String key) {
        String header = request.getHeader("7d");
        if ("7dGgroup".equals(header)) {
            redisTemplate.opsForValue().set(key, "压测流量");
            log.info("压测流量调试经过路线");
        } else {
            redisTemplate.opsForValue().set(key, "线上流量");
        }
    }
}
